package com.good.webadmin.Controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.good.domain.config.WebLogEnum;
import com.good.domain.controller.BaseController;
import com.good.domain.entity.AdminUser;
import com.good.domain.models.Result;
import com.good.domain.service.IAdminLogService;
import com.good.domain.service.IAdminUserService;
import com.good.domain.service.IUserLoginToken;
import com.good.webadmin.Models.Params.AdminLogModel;
import com.good.webadmin.Models.TokenUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author duyf
 * @since 2022-05-14
 */
@Api(tags = "操作日志")
@RestController
@RequestMapping("/webadmin/admin-log")
public class AdminLogController extends BaseController {

    @Autowired
    private IAdminLogService adminLogService;
    @Autowired
    private IAdminUserService adminUserService;

    @IUserLoginToken
    @PostMapping("logs")
    @ApiOperation("日志列表")
    public Result logs(TokenUser tokenUser, @RequestBody AdminLogModel model) {
        AdminUser adminUser = adminUserService.getById(tokenUser.getUserId());
        Long uid = null;
        if (!adminUser.getUserName().equals("admin")) {
            if (model.getUid() != null) {
                uid = model.getUid();
            }
        } else {
            uid = tokenUser.getUserId();
        }
        IPage<HashMap<String,Object>> page = new Page<>(model.getPageNum(), model.getPageSize());
        IPage<HashMap<String,Object>> adminLogPage = adminLogService.pageInfo(page, uid, model.getStartTime(),model.getEndTime(),model.getModuleId(),model.getKeywords());

        return Success(adminLogPage);
    }


    @IUserLoginToken
    @PostMapping("modules")
    @ApiOperation("模块列表")
    public Result modules() {
        List<HashMap<String, Object>> result = new ArrayList<>();
        WebLogEnum[] values = WebLogEnum.values();
        for (WebLogEnum value : values) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("url", value.getUrl());
            map.put("moduleId", value.getModuleId());
            map.put("moduleName", value.getModuleName());
            result.add(map);
        }
        return Success(result);
    }


}
